#if defined(ARCH_ARM)
#include <arm_neon.h>

// NEON优化的图像灰度化
ErrorCode image_grayscale_neon(const Image* src, Image** dest) {
    if (src->channels != 3) return ERR_INVALID_CHANNELS;
    
    Image* gray = image_create(src->width, src->height, 1);
    if (!gray) return ERR_MEMORY_ALLOC;
    
    const uint8_t* src_ptr = src->data;
    uint8_t* dst_ptr = gray->data;
    const size_t num_pixels = src->width * src->height;
    
    // NEON处理每次16个像素
    const uint8x16_t r_coeff = vdupq_n_u8(77);   // 0.299 * 256
    const uint8x16_t g_coeff = vdupq_n_u8(150);  // 0.587 * 256
    const uint8x16_t b_coeff = vdupq_n_u8(29);   // 0.114 * 256
    
    for (size_t i = 0; i < num_pixels; i += 16) {
        // 加载RGB数据
        uint8x16x3_t rgb = vld3q_u8(src_ptr);
        src_ptr += 48;
        
        // 计算灰度值: Y = 0.299R + 0.587G + 0.114B
        uint16x8_t low_r = vmull_u8(vget_low_u8(rgb.val[0]), r_coeff);
        uint16x8_t high_r = vmull_u8(vget_high_u8(rgb.val[0]), r_coeff);
        uint16x8_t low_g = vmull_u8(vget_low_u8(rgb.val[1]), g_coeff);
        uint16x8_t high_g = vmull_u8(vget_high_u8(rgb.val[1]), g_coeff);
        uint16x8_t low_b = vmull_u8(vget_low_u8(rgb.val[2]), b_coeff);
        uint16x8_t high_b = vmull_u8(vget_high_u8(rgb.val[2]), b_coeff);
        
        // 合并并计算灰度值
        uint16x8_t low_sum = vadd_u16(low_r, low_g);
        uint16x8_t high_sum = vadd_u16(high_r, high_g);
        high_sum = vadd_u16(high_sum, low_b);
        high_sum = vadd_u16(high_sum, high_b);
        // 右移8位并取低8位
        uint8x16_t gray_val = vcombine_u8(vrshrn_n_u16(low_sum, 8), vrshrn_n_u16(high_sum, 8));
        // 存储结果
        vst1q_u8(dst_ptr, gray_val);
        dst_ptr += 16;
    }
    
    *dest = gray;
    return SUCCESS;
}
#endif